/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package cn.edu.nju.software.greencreditsystem.action;

import cn.edu.nju.software.greencreditsystem.DTO.InputRecordListDTO;
import cn.edu.nju.software.greencreditsystem.DTO.OutputRecordDTO;
import cn.edu.nju.software.greencreditsystem.DTO.RepayRecordListDTO;
import cn.edu.nju.software.greencreditsystem.domain.Enterprise;
import cn.edu.nju.software.greencreditsystem.domain.EnterpriseType;
import cn.edu.nju.software.greencreditsystem.domain.RepaymentRecord;
import cn.edu.nju.software.greencreditsystem.factory.ServiceFactory;
import cn.edu.nju.software.greencreditsystem.service.EnterpriseService;
import cn.edu.nju.software.greencreditsystem.service.EnterpriseTypeService;
import cn.edu.nju.software.greencreditsystem.service.EnvironmentExcelService;
import cn.edu.nju.software.greencreditsystem.service.EnvironmentService;
import cn.edu.nju.software.greencreditsystem.service.FinancialExcelService;
import cn.edu.nju.software.greencreditsystem.service.FinancialService;
import cn.edu.nju.software.greencreditsystem.service.RepaymentRecordService;
import com.opensymphony.xwork2.ActionSupport;
import java.sql.Date;
import java.text.DecimalFormat;
import java.util.ArrayList;
import java.util.List;

/**
 * 作者：韩天
 * 创建时间：2011-8-11
 * 类描述：银行客户管理模块的action封装类
 */
public class BankCustomerAction extends ActionSupport {

    private List<EnterpriseType> typeList;//(所有)的企业类型
    private List<Enterprise> enterpriseList;//（同一分类下）企业的list
    private Enterprise enterprise;//企业实例
    private RepayRecordListDTO recordListDTO = new RepayRecordListDTO();//银行添加企业时添加还贷计划list的DTO(前台->后台)
    private List<RepaymentRecord> recordList;//还贷记录list
    private List<OutputRecordDTO>  outputRecordList;//银行查看企业时显示还贷记录的list(后台->前台)
    private InputRecordListDTO inputRecordListDTO = new InputRecordListDTO();//银行查看企业时更改还款记录list的DTO(前台->后台)
    private EnterpriseType type;//企业类型
    private String shi = "是";
    private String fou = "否";
    private int level;//企业的评级
    private double financialTotal;//企业财务总评
    private double environmentTotal;//企业环境总评

    private int beginYear;//企业开始提交报表的年份
    private int beginMonth;//企业开始提交报表的月份
    private int endYear;//企业截止提交报表的年份
    private int endMonth;//企业截止提交报表的月份
        
    @Override
    /**
     * 方法描述：默认的执行方法，实现无条件跳转
     * param：
     * return：SUCCESS
     */
    public String execute() {
        return SUCCESS;
    }
    
    /**
     * 方法描述：显示主界面
     * param：
     * return：SUCCESS
     */
    public String show(){
        //取得当前的所有企业类型
        EnterpriseTypeService ets = ServiceFactory.getEnterpriseTypeServiceInstance();
        this.typeList = ets.getTypeList();
        
        //取得当前分类所有的企业，如果当前分类还没有被赋值则取得所有企业
        EnterpriseService es = ServiceFactory.getEnterpriseServiceInstance();
        if(this.type == null){
            type = new EnterpriseType();
            type.setId(-1);
            type.setType("all");
        }
        this.enterpriseList = es.getEnterpriseListByType(type);
        
        return SUCCESS;        
    }
    
    /**
     * 方法描述：显示添加企业界面
     * param：
     * return：SUCCESS
     */
    public String show_addEnterprise(){
        //取得当前的所有企业类型
        EnterpriseTypeService ets = ServiceFactory.getEnterpriseTypeServiceInstance();
        this.typeList = ets.getTypeList();
        return SUCCESS;
    }
    
    /**
     * 方法描述：添加一个企业
     * param：
     * return：SUCCESS
     */
    public String add_Enterprise(){
        //取得企业类型对象
        String typeName = type.getType();
        EnterpriseTypeService ets = ServiceFactory.getEnterpriseTypeServiceInstance();
        this.type = ets.getTypeByName(typeName);
        enterprise.setEnterpriseType(type);
        
        //添加一条enterprise记录
        EnterpriseService es = ServiceFactory.getEnterpriseServiceInstance();
        es.add(enterprise);
        this.enterprise = es.getEnterpriseByUsername(enterprise.getUsername());
        
        //逐条添加还贷记录
        RepaymentRecordService rrs = ServiceFactory.getRepaymentRecordServiceInstance();
        for(int i=1; i<100; i++){
            if(recordListDTO.time[i] != null){
                RepaymentRecord tempRecord = new RepaymentRecord();
                //设置还贷记录的计划日期
                tempRecord.setPlanDate(new Date(Integer.parseInt(recordListDTO.time[i].substring(0, 4))-1900,
                        Integer.parseInt(recordListDTO.time[i].substring(5, 7))-1,
                        Integer.parseInt(recordListDTO.time[i].substring(8, 10))));
                //设置还贷记录的金额
                tempRecord.setAmount(Double.parseDouble(recordListDTO.amount[i]));
                rrs.addPlan(enterprise.getId(), tempRecord);
            }
        }
        recordListDTO = new RepayRecordListDTO();
        
        return SUCCESS;
    }
    
    /**
     * 方法描述：银行添加企业时添加的企业遵守的上传计划
     * param：
     * return：SUCCESS
     */
    public String add_UploadPlan(){
        //添加财务报表
        FinancialExcelService fes = ServiceFactory.getFinancialExcelServiceInstance();
        fes.add_UploadPlan(enterprise.getId(), beginYear, beginMonth, endYear, endMonth);
        
        //添加环境报表
        EnvironmentExcelService ees = ServiceFactory.getEnvironmentExcelServiceInstance();
        ees.add_UploadPlan(enterprise.getId(), beginYear, beginMonth, endYear, endMonth);
        return SUCCESS;
    }
    
    /**
     * 方法描述：添加一个企业类型
     * param：
     * return：SUCCESS
     */
    public String add_EnterpriseType(){
        EnterpriseTypeService ets = ServiceFactory.getEnterpriseTypeServiceInstance();
        String typeName = this.type.getType();
        this.type = new EnterpriseType();
        this.type.setType(typeName);
        ets.add(this.type);
        return SUCCESS;
    }
    
    /**
     * 方法描述：修改（更新）一个企业的类型
     * param：
     * return：SUCCESS
     */
    public String update_EnterpriseType(){
        EnterpriseService es = ServiceFactory.getEnterpriseServiceInstance();
        EnterpriseTypeService ets = ServiceFactory.getEnterpriseTypeServiceInstance();
        //取得type
        this.type = ets.getTypeByName(type.getType());
        //取得企业
        this.enterprise = es.getEnterpriseById(enterprise.getId());
        enterprise.setEnterpriseType(type);
        
        es.update(enterprise);
        return SUCCESS;
    }
    
    /**
     * 方法描述：更新一个企业
     * param：
     * return：SUCCESS
     */
    public String lookup_Enterprise(){
        //取得企业基本信息
        EnterpriseService es = ServiceFactory.getEnterpriseServiceInstance();
        this.enterprise = es.getEnterpriseById(enterprise.getId());
        
        //取得企业还贷记录list
        RepaymentRecordService rrs = ServiceFactory.getRepaymentRecordServiceInstance();
        this.recordList = rrs.getRecordList(enterprise.getId());
        
        //将recordList中的内容放入inputRecordListDTO中方便前台显示
        
        this.outputRecordList = new ArrayList<OutputRecordDTO>();
        for(int i=0; i<recordList.size(); i++){
            OutputRecordDTO temp = new OutputRecordDTO();
            //计划还贷日期
            temp.setPlanDate(recordList.get(i).getPlanDate().toString());
            //还款金额
            temp.setAmount(recordList.get(i).getAmount());
            //是否已还
            if(recordList.get(i).getHasPaid() == true){
                temp.setHasPaid("是");
            }else{
                temp.setHasPaid("否");
            }
            //真实还款日期
            if(recordList.get(i).getRealDate() != null){
                temp.setRealDate(recordList.get(i).getRealDate().toString());
            }else{
                temp.setRealDate("");
            }
            
            this.outputRecordList.add(temp);
        }
        
        //取得企业评分和评级
        FinancialService fs = ServiceFactory.getFinancialServiceInstance();
        EnvironmentService us = ServiceFactory.getEnvironmentServiceInstance();
        this.level = enterprise.getLevel();
        this.financialTotal = fs.get_Total(enterprise.getId());
        this.environmentTotal = us.get_Total(enterprise.getId());
        
        return SUCCESS;
    }
    
    /**
     * 方法描述：更新一个企业的还贷记录
     * param：
     * return：SUCCESS
     */
    public String update_RepaymentRecord(){
        //先删除所有该企业以前的还贷记录
        RepaymentRecordService rrs = ServiceFactory.getRepaymentRecordServiceInstance();
        rrs.deleteAllById(enterprise.getId());
        
        //逐条添加新的还贷记录
        for(int i=1; i<100; i++){
            if((inputRecordListDTO.planDate[i] != null) && (inputRecordListDTO.planAmount[i] != 0) &&
                    (inputRecordListDTO.isRepaid[i] != null) ){
                RepaymentRecord tempRecord = new RepaymentRecord();
                
                tempRecord.setId(enterprise.getId());
                
                //设置还贷记录的计划日期
                tempRecord.setPlanDate(new Date(Integer.parseInt(inputRecordListDTO.planDate[i].substring(0, 4))-1900,
                        Integer.parseInt(inputRecordListDTO.planDate[i].substring(5, 7))-1,
                        Integer.parseInt(inputRecordListDTO.planDate[i].substring(8, 10))));
                
                //设置还贷记录的金额
                tempRecord.setAmount(inputRecordListDTO.planAmount[i]);
                
                //设置还贷记录的是否已还
                if(inputRecordListDTO.isRepaid[i].equals("是") ){
                    tempRecord.setHasPaid(true);
                }else{
                    tempRecord.setHasPaid(false);
                }
                
                //设置还贷记录的真实还贷时间
                if(inputRecordListDTO.realDate[i] != null && !(("").equals(inputRecordListDTO.realDate[i]))){
                    tempRecord.setRealDate(new Date(Integer.parseInt(inputRecordListDTO.realDate[i].substring(0, 4))-1900,
                        Integer.parseInt(inputRecordListDTO.realDate[i].substring(5, 7))-1,
                        Integer.parseInt(inputRecordListDTO.realDate[i].substring(8, 10))));
                }
                //将记录添加
                rrs.addPlan(enterprise.getId(), tempRecord);
            }
        }
        return SUCCESS;
    }
    
    
    
    
    public List<Enterprise> getEnterpriseList() {
        return enterpriseList;
    }

    public void setEnterpriseList(List<Enterprise> enterpriseList) {
        this.enterpriseList = enterpriseList;
    }

    public List<EnterpriseType> getTypeList() {
        return typeList;
    }

    public void setTypeList(List<EnterpriseType> typeList) {
        this.typeList = typeList;
    }
    
    public Enterprise getEnterprise() {
        return enterprise;
    }

    public void setEnterprise(Enterprise enterprise) {
        this.enterprise = enterprise;
    }

    public List<RepaymentRecord> getRecordList() {
        return recordList;
    }

    public void setRecordList(List<RepaymentRecord> recordList) {
        this.recordList = recordList;
    }

    public RepayRecordListDTO getRecordListDTO() {
        return recordListDTO;
    }

    public void setRecordListDTO(RepayRecordListDTO recordListDTO) {
        this.recordListDTO = recordListDTO;
    }

    public EnterpriseType getType() {
        return type;
    }

    public void setType(EnterpriseType type) {
        this.type = type;
    }

    public InputRecordListDTO getInputRecordListDTO() {
        return inputRecordListDTO;
    }

    public void setInputRecordListDTO(InputRecordListDTO inputRecordListDTO) {
        this.inputRecordListDTO = inputRecordListDTO;
    }

    public List<OutputRecordDTO> getOutputRecordList() {
        return outputRecordList;
    }

    public void setOutputRecordList(List<OutputRecordDTO> outputRecordList) {
        this.outputRecordList = outputRecordList;
    }
    
    public String getFou() {
        return fou;
    }

    public void setFou(String fou) {
        this.fou = fou;
    }

    public String getShi() {
        return shi;
    }

    public void setShi(String shi) {
        this.shi = shi;
    }
        
    public int getBeginMonth() {
        return beginMonth;
    }

    public void setBeginMonth(int beginMonth) {
        this.beginMonth = beginMonth;
    }

    public int getBeginYear() {
        return beginYear;
    }

    public void setBeginYear(int beginYear) {
        this.beginYear = beginYear;
    }

    public int getEndMonth() {
        return endMonth;
    }

    public void setEndMonth(int endMonth) {
        this.endMonth = endMonth;
    }

    public int getEndYear() {
        return endYear;
    }

    public void setEndYear(int endYear) {
        this.endYear = endYear;
    }

    public double getEnvironmentTotal() {
        return environmentTotal;
    }

    public void setEnvironmentTotal(double environmentTotal) {
        this.environmentTotal = environmentTotal;
    }

    public double getFinancialTotal() {
        return financialTotal;
    }

    public void setFinancialTotal(double financialTotal) {
        this.financialTotal = financialTotal;
    }

    public int getLevel() {
        return level;
    }

    public void setLevel(int level) {
        this.level = level;
    }
}
